import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/page/me/my_house_controller.dart';
import 'package:kkhousekeep/widget/custom_button.dart';
import 'package:kkhousekeep/widget/house_item.dart';

import '../../widget/custom_app_bar.dart';
import '../authorization/authorization_page.dart';

class MyHouse extends StatelessWidget {
  const MyHouse({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MyHouseController>(
      init: MyHouseController(),
      builder: (controller) => Scaffold(
        appBar: CustomAppBar("我的房屋", actions: [_buildAppBarNotificationBtn()]),
        body: Container(
          width: double.infinity,
          color: const Color(0xFFf4f5f6),
          child: SafeArea(
            left: false,
            right: false,
            child: Stack(
              alignment: Alignment.topCenter,
              children: [
                Obx(() => _builderContentWidget(controller)),
                _buildAddHouseBtn(context, controller),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildAppBarNotificationBtn() {
    MyHouseController controller = Get.find();
    return TextButton(
      onPressed: () => controller.buildAppBarNotificationBtn(),
      child: Obx(
        () => Image(
          image: controller.hasNewMsg.value
              ? const AssetImage(
                  'assets/images/icon_my_house_notification_red_dot.png')
              : const AssetImage(
                  'assets/images/icon_my_house_notification.png'),
          width: 48.h,
          height: 48.h,
          fit: BoxFit.contain,
        ),
      ),
    );
  }

  Widget _buildOtherRoomsListWidget(MyHouseController controller) {
    return Expanded(
      child: Padding(
        padding: EdgeInsets.only(left: 32.w, right: 32.w),
        child: ListView.separated(
            itemBuilder: (context, index) => HouseItem(
                  controller.rooms[index],
                  isCurrentHouse: false,
                  onTap: () {
                    controller.switchHouse(controller.rooms[index]);
                  },
                ),
            separatorBuilder: (context, index) => Divider(
                  height: 16.h,
                  thickness: 0,
                  indent: 0,
                  color: Colors.transparent,
                ),
            itemCount: controller.rooms.length),
      ),
    );
  }

  Widget _buildCategoryItem(String text) {
    return Padding(
        padding: EdgeInsets.only(left: 64.w, top: 32.h, bottom: 16.h),
        child: Text(
          text,
          style: TextStyle(color: const Color(0xFF333333), fontSize: 32.sp),
        ));
  }

  Widget _buildAddHouseBtn(BuildContext context, MyHouseController controller) {
    return Positioned(
        bottom: 60.h,
        child: CustomButton(
          isAsh: false,
          width: 420.w,
          height: 90.h,
          text: "+  新增房屋",
          onPressed: () {
            Get.to(const BuildAuthorizationPage());
          },
        ));
  }

  Widget _builderContentWidget(MyHouseController controller) {
    bool hasRoom = controller.currentRoom.value.roomId != -1;
    return hasRoom ? _builderRoomsWidget(controller) : _builderEmptyWidget();
  }

  Widget _builderRoomsWidget(MyHouseController controller) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _buildCategoryItem('当前房屋'),
        Container(
          margin: EdgeInsets.only(left: 32.w, right: 32.w),
          child: HouseItem(controller.currentRoom.value, isCurrentHouse: true),
        ),
        Visibility(
          visible: controller.rooms.isNotEmpty,
          child: _buildCategoryItem('其他房屋'),
        ),
        _buildOtherRoomsListWidget(controller),
      ],
    );
  }

  Widget _builderEmptyWidget() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Image.asset(
          "assets/images/authorization_community_not_found.png",
          width: 288.w,
          height: 358.w,
        ),
        Text("暂无房屋",
            style: TextStyle(color: const Color(0xFF999999), fontSize: 28.sp))
      ],
    );
  }
}
